package app.netant.bt.btcat;

import cn.lily.com.db.DBFactory;
import cn.lily.com.lang.number.MathUtil;

/**
 * BtCat操作实体类
 * @author JiaJ
 * @date 2018年6月27日
 *
 */
public class BtCatEntity {
	
	private String resID = System.currentTimeMillis()+MathUtil.random(3);
	private String resNameSim;
	private String resName;
	private String resThunder;
	private String resSeed;
	private String resMargnet;
	private String resUrl;
	private String fileSize;
	private String fileNum;
	private String fileDate;
	private String resDate;
	
	private String resType;
	private String fileList;
	private String resTags;

	public void setResNameSim(String resName) {
		this.resNameSim = this.getString(cutString(resName.trim(), '(', ')').trim());
	}

	public void setResName(String resName) {
		this.resName = this.getString(resName.trim());
	}
	public void setResUrl(String resUrl) {
		this.resUrl = resUrl;
	}

	public void setFileSize(String fileSize) {
		this.fileSize = fileSize;
	}

	public void setFileNum(String fileNum) {
		this.fileNum = fileNum;
	}
	public void setResDate(String resDate) {
		this.resDate = resDate;
	}
	
	public void setTags(String tagName,String tagValue){
		
		tagName = tagName.trim();
		tagValue = this.getString(tagValue.trim());
		
		if("文件大小".equals(tagName)){
			this.fileSize = tagValue;
		}
		
		else if("文件数量".equals(tagName)){
			this.fileNum = tagValue;
		}
		
		else if("创建时间".equals(tagName)){
			this.fileDate = tagValue;
		}
		
		else if("文件类型".equals(tagName)){
			this.resType = tagValue;
		}
		
		else if("磁力链接".equals(tagName)){
			this.resMargnet = tagValue;
		}
		
		else if("种子下载".equals(tagName)){
			this.resSeed = tagValue;
		}
		
		else if("迅雷下载".equals(tagName)){
			this.resThunder = tagValue;
		}
		
		else if("相关链接".equals(tagName)){
			this.resTags = tagValue;
		}
		
		else if("文件列表".equals(tagName)){
			this.fileList = tagValue;
		}
		
		
	}
	
	public void insert(){
		
		if(this.resName==null || "".equals(this.resName)){
			return;
		}
		
		String sql = "insert into ${schema}.LEO_BTCAT_RES_INFO "
				   + "(RES_ID,RES_NAME_SIM,RES_NAME,RES_URL,RES_MARGNET,"
				   + "RES_DATE,RES_TYPE,RES_TAG,RES_SEED,RES_THUNDER,"
				   + "FILE_SIZE,FILE_NUM,FILE_DATE,FILE_LIST) "
				   + "values "
				   + "('"+this.resID+"','"+this.resNameSim+"','"+this.resName+"','"+this.resUrl+"','"+this.resMargnet+"',"
				    + "'"+this.resDate+"','"+this.resType+"','"+this.resTags+"','"+this.resSeed+"','"+this.resThunder+"',"
				    + "'"+this.fileSize+"','"+this.fileNum+"','"+this.fileDate+"','"+this.fileList+"')";
		
		try {
			DBFactory.getThreadDB().executeUpdate(sql);
			DBFactory.getThreadDB().commit();
		} catch (Exception e) {
			e.printStackTrace();
			DBFactory.getThreadDB().executeUpdate("insert into ${schema}.LEO_BTCAT_RES_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
					+ "values ('"+System.currentTimeMillis()+MathUtil.random(3)+"','"+this.resUrl+"','2')");
		}

	}
	
	public static String cutString(String name,char begin,char end){
		
		StringBuffer sb = new StringBuffer();
		boolean flag = true;
		for(int i=0;i<name.length();i++){
			char active = name.charAt(i);
			
			if(active == begin){
				flag = false;
			}else if(active == end){
				flag = true;
			}else{
				if(flag){
					sb.append(active);
				}
			}
			
		}
		
		return sb.toString();
		
	}
	
	protected String getString(String src){
		return src.replaceAll("'", "’")
				  .replaceAll("\"", "”")
				  .replaceAll("&nbsp;", "")
				  .replaceAll("<br>", "")
				  .replaceAll("<br/>", "")
				  .replaceAll("<p>", "")
				  .replaceAll("</p>", "");
	}
	
}
